package com.tetris.project;

import views.GamePlayView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.Toast;
import engine.Piece;
import framework.Physics;
import framework.interfaces.Movable.Side;
import framework.interfaces.Rotable.Direction;

public class Game extends Activity {
		Music buttonSound;
		KeyEvent myKey;
		GamePlayView gamePlayView;
		float ini_xm_mouse, final_xm_mouse;
		float ini_ym_mouse, final_ym_mouse;
		float tapTime;
		public static boolean gameOver;
		final String gameStatus = "/data/data/com.tetris.project/mGame.bin";
		GameStatus mGameStatus;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(Shr_Res.TAG, "Start Game.onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gameplay_layout);
		gamePlayView = (GamePlayView) findViewById(R.id.gameplayview);
		buttonSound = new Music(this);
		gameOver = false;
		mGameStatus = new GameStatus();
		//TetrisPieceManager.firstTime=true;
		Log.d(Shr_Res.TAG, "End Game.onCreate");
	}

	public void onStart(){
		Log.d(Shr_Res.TAG, "Start Game.onStart");
		super.onStart();
		Shr_Res.gameMusic = new Music(this);
		Log.d(Shr_Res.TAG, "End Game.onStart");
	}

	protected void onResume() {
		Log.d(Shr_Res.TAG, "Start Game.onResume");
		super.onResume();

		if(Shr_Res.music) {
			Shr_Res.gameMusic.play(R.raw.game);
		} else {
			Shr_Res.gameMusic.stop();
		}
		Shr_Res.screen = false;
		if (Game.gameOver){
			finish();
		}
		
		/*
		 * Here We will open&analize the Game status file to restore the game conditions saved
		 * just to open some stuff...
		 */
		Log.d(Shr_Res.TAG, "End Game.onResume");
	}

	protected void onPause() {
		Log.d(Shr_Res.TAG, "Start Game.onPause");
		super.onPause();

		/* Here, We will save the game status by checking if
		 * this was not a Game Over.
		 */

		if (gameOver) {
			
		}
		Log.d(Shr_Res.TAG, "End Game.onPause");
	}

	protected void onStop() {
		Log.d(Shr_Res.TAG, "Start Game.onStop");
		super.onStop();
		Shr_Res.gameMusic.stop();
		Log.d(Shr_Res.TAG, "End Game.onStop");
	}

	protected void onDestroy() {
		Log.d(Shr_Res.TAG, "Start Game.onDestroy");
		super.onDestroy();
		Shr_Res.firstTime = true;
		Shr_Res.secondTime = true;
		Log.d(Shr_Res.TAG, "End Game.onDestroy");
	}

	protected void onRestart() {
		Log.d(Shr_Res.TAG, "Start Game.onRestart");
		super.onRestart();
		Log.d(Shr_Res.TAG, "End Game.onRestart");
	}

/*    public boolean dispatchKeyEvent(KeyEvent event){
    	Log.d(Shr_Res.TAG, "A Keyboard Key was pressed");
    	int keyCode = event.getKeyCode();
    	if (event.getAction() == KeyEvent.ACTION_UP){
    	switch (keyCode) {
    	case KeyEvent.KEYCODE_DPAD_LEFT:
    		Log.d(Shr_Res.TAG, "Dpad left was pressed");
    		if(Shr_Res.sfx){buttonSound.playButtonSound(R.raw.chimes);}
    		break;
    	case KeyEvent.KEYCODE_DPAD_DOWN:
    		Log.d(Shr_Res.TAG, "Dpad down was pressed");
    		if(Shr_Res.sfx){buttonSound.playButtonSound(R.raw.chimes);}
    		break;
    	case KeyEvent.KEYCODE_ENTER:
    		Log.d(Shr_Res.TAG, "Dpad center was pressed");
    		if(Shr_Res.sfx){buttonSound.playButtonSound(R.raw.chimes);}
    		break;
    	case KeyEvent.KEYCODE_DPAD_RIGHT:
    		Log.d(Shr_Res.TAG, "Dpad right was pressed");
    		if(Shr_Res.sfx){buttonSound.playButtonSound(R.raw.chimes);}
    		break;
    	case KeyEvent.KEYCODE_BACK:
    		Log.d(Shr_Res.TAG, "Back was pressed");
    		onBackPressed();
    		break;    		
    	}}
    	return true;
    }*/

	public boolean onTouchEvent (MotionEvent evt){
		int action = evt.getAction();
		int gridwidth = gamePlayView.gameThread.mScene.mCurrentPiece.getUnitPieceWidth();
		Log.d(Shr_Res.TAG, "Square Grid Width ="+gridwidth);
		
		if(action == MotionEvent.ACTION_DOWN){
			tapTime = evt.getEventTime();
			Log.d(Shr_Res.TAG, "Start Tap Time="+tapTime);
			ini_xm_mouse = evt.getX();
			ini_ym_mouse = evt.getY()-getYRelative();
			final_xm_mouse = evt.getX();
			final_ym_mouse = evt.getY()-getYRelative();
			Shr_Res.touchXPosition = evt.getX();
			Shr_Res.touchYPosition = evt.getY()-getYRelative();
			Shr_Res.screenTouched = true;
			Log.d(Shr_Res.TAG, "Ini Mouse Pos x ="+ini_xm_mouse);
			Log.d(Shr_Res.TAG, "Ini Mouse Pos y ="+ini_ym_mouse);
		}
		
		if(action == MotionEvent.ACTION_UP){
			tapTime = evt.getEventTime()-tapTime;
			Log.d(Shr_Res.TAG, "End Tap Time="+tapTime);
			Shr_Res.touchXPosition = evt.getX();
			Shr_Res.touchYPosition = evt.getY()-getYRelative();
			Log.d(Shr_Res.TAG, "Touch x ="+Shr_Res.touchXPosition);
			Log.d(Shr_Res.TAG, "Touch y ="+Shr_Res.touchYPosition);
			Shr_Res.screenTouched = false;

			//Check if the Menu button was pressed and starts the Options activity
			if(gamePlayView.gameThread.mScene.mBoardUI.menu.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition)){
				showMenu();
			}

			if(gamePlayView.gameThread.mScene.mBoardUI.pause.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition)){
				Log.d("pauseButton","Pause Button Pressed");
				if (Shr_Res.pauseGame){   //Game was unpaused
					Physics.setResumedTime();
					Shr_Res.pauseGame = false;
					Log.d("pauseButton","Shr_Res.pauseGame = "+Shr_Res.pauseGame);
				}
				else if (!Shr_Res.pauseGame){   //Game was paused
					Shr_Res.pauseGame = true;
					Physics.setPausedTime(Physics.gameTime);
					Log.d("pauseButton","Shr_Res.pauseGame = "+Shr_Res.pauseGame);
				}
			}

			//Checks if the Menu and Pause buttons were not pressed and if tap time was less than 150 ms, if true it rotates the piece
			if(!gamePlayView.gameThread.mScene.mBoardUI.pause.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition) &&
				(!gamePlayView.gameThread.mScene.mBoardUI.menu.isButtonPressed(Shr_Res.touchXPosition, Shr_Res.touchYPosition)) &&
					(!Shr_Res.pauseGame) && //if the game was paused don't move the piece)
						(tapTime <= 150f)  && !Shr_Res.sendDownPiece) {
				while (Piece.isRotateRunning){
					Log.d("--- Rotate ---","Rotating is busy");
				}
				Log.d("--- Rotate ---","I'm going to rotate");
				gamePlayView.gameThread.mScene.getPiece().rotate(Direction.CLOCKWISE);					
				
			}
		}

		if(action == MotionEvent.ACTION_MOVE) {
			Shr_Res.touchXPosition = evt.getX();
			Shr_Res.touchYPosition = evt.getY()-getYRelative();
			final_xm_mouse = evt.getX();
			final_ym_mouse = evt.getY()-getYRelative();
			if (ini_xm_mouse < final_xm_mouse)  //Gesture was left to right
				if(final_xm_mouse-ini_xm_mouse>gridwidth) {
					if(!Shr_Res.pauseGame  && !Shr_Res.sendDownPiece) {//if the game was paused don't move the piece
						gamePlayView.gameThread.mScene.getPiece().move(Side.RIGHT);
						ini_xm_mouse=final_xm_mouse;
					}
				}
			if (ini_xm_mouse > final_xm_mouse)  //Gesture was right to left
				if(ini_xm_mouse-final_xm_mouse>gridwidth) {
					if(!Shr_Res.pauseGame && !Shr_Res.sendDownPiece) {//if the game was paused don't move the piece
						gamePlayView.gameThread.mScene.getPiece().move(Side.LEFT);
						ini_xm_mouse=final_xm_mouse;
					}
				}
			if (final_ym_mouse > ini_ym_mouse)  //Gesture was up to down
				if(final_ym_mouse - ini_ym_mouse > gridwidth*3)
					if(!Shr_Res.pauseGame) {//if the game was paused don't move the piece
						Log.d(Shr_Res.TAG, "Setting sendDownPiece = true");
						Shr_Res.sendDownPiece = true;
						//gamePlayView.gameThread.mScene.getPiece().update(final_ym_mouse-ini_ym_mouse);
					}
			Log.d(Shr_Res.TAG, "Moving x ="+final_xm_mouse);
			Log.d(Shr_Res.TAG, "Moving y ="+final_ym_mouse);
			//watchPath(xm_mouse, ym_mouse);
		}
		return true;
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    	case R.id.options:
	    	startActivity(new Intent(this, Options.class));
	    	return true;
    	// More items go here (if any) ...
    	}
    	return false;
	}
    
    public int getYRelative(){ //returns size of the title bar plus status bar
        Window window = getWindow();
        return window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	switch (keyCode) {
    	case KeyEvent.KEYCODE_DPAD_LEFT:
    		Log.d(Shr_Res.TAG, "Dpad left was pressed");
    		if (!Shr_Res.pauseGame){ //if the game was paused don't move the piece)
    			if(Shr_Res.sfx){buttonSound.playButtonSound(R.raw.chimes);}
    			gamePlayView.gameThread.keyDown(keyCode);
    		}
    		break;
    	case KeyEvent.KEYCODE_DPAD_CENTER:
    		Log.d(Shr_Res.TAG, "Dpad center was pressed");
    		if (!Shr_Res.pauseGame && !Shr_Res.sendDownPiece){ //if the game was paused don't move the piece)
    			if(Shr_Res.sfx){buttonSound.playButtonSound(R.raw.chimes);}
    			gamePlayView.gameThread.keyDown(keyCode);
    		}
    		break;
    	case KeyEvent.KEYCODE_DPAD_RIGHT:
    		Log.d(Shr_Res.TAG, "Dpad right was pressed");
    		if (!Shr_Res.pauseGame && !Shr_Res.sendDownPiece){ //if the game was paused don't move the piece)
    			if(Shr_Res.sfx){buttonSound.playButtonSound(R.raw.chimes);}
    			gamePlayView.gameThread.keyDown(keyCode);
    		}
    		break;
    	case KeyEvent.KEYCODE_DPAD_DOWN:
    		Log.d(Shr_Res.TAG, "Dpad down was pressed");
    		Shr_Res.sendDownPiece = true;
    		break;
    	case KeyEvent.KEYCODE_Q:
    		Log.d(Shr_Res.TAG, "Q was pressed");
    		if (!Shr_Res.pauseGame && !Shr_Res.sendDownPiece){ //if the game was paused don't move the piece)
    			if(Shr_Res.sfx){buttonSound.playButtonSound(R.raw.chimes);}
    			gamePlayView.gameThread.keyDown(keyCode);
    		}
    		break;
    	case KeyEvent.KEYCODE_W:
    		Log.d(Shr_Res.TAG, "W was pressed");
    		if (!Shr_Res.pauseGame){ //if the game was paused don't move the piece)
    			if(Shr_Res.sfx){buttonSound.playButtonSound(R.raw.chimes);}
    			gamePlayView.gameThread.keyDown(keyCode);
    		}
    		break;
    	case KeyEvent.KEYCODE_SPACE:
			Log.d("pauseButton","Space Button Pressed");
			if (Shr_Res.pauseGame){   //Game was unpaused
				Physics.setResumedTime();
				Shr_Res.pauseGame = false;
				Log.d("pauseButton","Shr_Res.pauseGame = "+Shr_Res.pauseGame);
			}
			else if (!Shr_Res.pauseGame){   //Game was paused
				Shr_Res.pauseGame = true;
				Physics.setPausedTime(Physics.gameTime);
				Log.d("pauseButton","Shr_Res.pauseGame = "+Shr_Res.pauseGame);
			}
    		break;
    	case KeyEvent.KEYCODE_MENU:
    		Log.d(Shr_Res.TAG, "Menu was pressed");
    		openOptionsMenu();
    		break;
    	default:
    		super.onKeyDown(keyCode, event);
    	}
    	return true;
    }
    
    public void showMenu(){
    	startActivity(new Intent(this, Options.class));
    }
}
